<?php


namespace App\Services;

use App\Common\Libs\Dispatch\Queue;
use App\Consts\GlobalConst;
use App\Consts\GlobalQueueConst;
use App\Jobs\Controller\LogCounterJob;

class LogCountService extends BaseService
{
    private $_repository;

    public function __construct()
    {
        parent::__construct();
        $this->_repository = repository()->LogCountRepository;
    }

    /**
     * 添加日志
     * @param $loadData
     * @return \App\Models\LogCount|bool|\Illuminate\Database\Eloquent\Model
     */
    public function createLog($loadData)
    {
        return $this->_repository->createLogCount($loadData);
    }

    /**
     * 使用队列异步添加日志
     * @param $loadData
     * @return \Illuminate\Foundation\Bus\PendingDispatch
     */
    public function createLogByQueue($loadData)
    {
        return Queue::getInstance(LogCounterJob::class)->push($loadData, GlobalQueueConst::LOG_COUNTER_CREATE);
    }

    /**
     * 通过【评论】获取【统计日志】所需数据
     * @param $comment
     * @param $actionType
     * @return array|bool
     */
    public function getLoadDataByComment($comment, $actionType)
    {
        $receiveUserId = service()->CommentService->getTargetUserId($comment);
        if (!$receiveUserId) {
            return false;
        }

        return [
            'send_user_id' => $comment['user_id'],
            'receive_user_id' => $receiveUserId,
            'action_id' => $comment['comment_id'],
            'action_type' => $actionType,
            'target_id' => $comment['target_id'],
            'target_type' => $comment['target_type'],
            'created_at' => time(),
        ];
    }

    /**
     * 通过【回复】获取【统计日志】所需数据
     * @param $reply
     * @param $actionType
     * @return array|bool
     */
    public function getLoadDataByReply($reply, $actionType)
    {
        $receiveUserId = service()->ReplyService->getReplyTargetUserId($reply);
        if (!$receiveUserId) {
            return false;
        }

        return [
            'send_user_id' => $reply['user_id'],
            'receive_user_id' => $receiveUserId,
            'action_id' => $reply['reply_id'],
            'action_type' => $actionType,
            'target_id' => $reply['target_id'],
            'target_type' => $reply['target_type'],
            'created_at' => time(),
        ];
    }

    /**
     * 获取主体用户ID
     * @param $target
     * @return bool|string
     */
    private function getTargetUserId($target)
    {
        $receiveUserId = '';
        if ((int)$target['target_type'] === GlobalConst::TARGET_TYPE_NOTE) {
            $note = repository()->NoteRepository->getNoteByNoteId($target['target_id']);
            if ($note) {
                $receiveUserId = $note['user_id'];
            }
        }
        if ((int)$target['target_type'] === GlobalConst::TARGET_TYPE_DYNAMIC) {
            $dynamic = repository()->DynamicRepository->getDynamicById($target['target_id']);
            if ($dynamic) {
                $receiveUserId = $dynamic['user_id'];
            }
        }

        return $receiveUserId ?? false;
    }
}
